﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Security.Principal;

namespace JSC.Portal.Entities
{
    public partial class PortalUser : BaseEntity, IIdentity
    {
        public string FullName
		{
			get { return string.Format("{0} {1}", Firstname, Lastname).Trim(); }
		}

        #region IIdentity Members

        public string AuthenticationType
        {
            get { return "Chompff.Portal.Authentication"; }
        }

        public bool IsAuthenticated
        {
            get;
            set;
        }

        public string Name
        {
            get
            {
                if (IsAuthenticated)
                    return Id.ToString();
                else
                    return string.Empty;
            }
        }

        #endregion

        /// <summary>
        /// Indicates if the user has edit permissions for a certain Section.
        /// </summary>
        /// <param name="section"></param>
        /// <returns></returns>
        public bool EditAllowed(Section section)
        {
            return (section.SectionPermissions.Count(p => p.Section.Id == section.Id && p.EditAllowed && IsInRole(p.Role)) > 0);
        }

        /// <summary>
        /// Indicates if the user has view permissions for a certain Node.
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public bool CanView(PortalPage page)
        {
            return (page.PagePermissions.Count(ppp => ppp.Page.Id == page.Id && ppp.ViewAllowed && IsInRole(ppp.Role)) > 0);
        }

        /// <summary>
        /// Indicates if the user has view permissions for a certain Section.
        /// </summary>
        /// <param name="section"></param>
        /// <returns></returns>
        public bool CanView(Section section)
        {
            return (section.SectionPermissions.Count(p => p.Section.Id == section.Id && p.ViewAllowed && IsInRole(p.Role)) > 0);
        }

        /// <summary>
        /// Determine if the user is in a give Role.
        /// </summary>
        /// <param name="roleToCheck"></param>
        /// <returns></returns>
        public bool IsInRole(Role roleToCheck)
        {
            return (Roles.Count(r => r.RoleEnum == roleToCheck.RoleEnum) > 0);
        }

        public bool IsInRole(Roles roleToCheck)
        {
            return (Roles.Count(r => r.RoleEnum == roleToCheck) > 0);
        }

        public bool IsInRole(params Roles[] roles)
        {
            var aantal = roles.Join(Roles, e => e, r => r.RoleEnum, (e, r) => new { e, r});
            return (aantal != null) ? (aantal.Count() > 0) : false;
        }

        ///// <summary>
        ///// Determine if the user is in a give Role.
        ///// </summary>
        ///// <param name="roleName"></param>
        ///// <returns></returns>
        //public bool IsInRole(string roleName)
        //{
        //    foreach (Role role in Roles)
        //    {
        //        if (role.Name.ToUpper().Equals(roleName.ToUpper()))
        //        {
        //            return true;
        //        }
        //    }
        //    return false;
        //}

        //public bool IsInRoles(string roleNames)
        //{
        //    string[] roles = roleNames.Split(',', ';');
        //    foreach (string roleName in roles)
        //        return IsInRole(roleName.Trim());
        //    return false;
        //}
    }
}
